package com.njlime.sys.repository;

import com.njlime.core.base.repository.impl.BaseDaoImpl;
import com.njlime.core.helper.PageBean;
import com.njlime.core.utils.StringUtil;
import com.njlime.sys.entities.User;
import com.njlime.sys.security.util.UserUtils;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017-7-18.
 */
@Repository
public class UserDaoImpl extends BaseDaoImpl<User> {

    /**
     * 根据登录名获取用户
     *
     * @param loginName
     * @return
     */
    public User getByLoginName(String loginName) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("select t.* from t_user t join t_unit u on u.id = t.unit_id " +
                "where t.is_del = 0 and t.status = 1 and u.is_del = 0 and u.status = 1 and t.login_name = ? ");
        arrList.add(loginName);
        List<User> re = findList(sb.toString(), arrList);
        if (re != null && re.size() > 0) {
            return re.get(0);
        }
        return null;
    }

    /**
     * 获取分页用户信息
     *
     * @param pageBean
     * @param user
     * @return
     */
    public PageBean list(PageBean pageBean, User user) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("select t.*,IFNULL(fu.unit_name,u.unit_name) first_unit,su.unit_name second_unit from t_user t ");
        sb.append("join t_unit u on u.id = t.unit_id ");
        sb.append("left JOIN t_unit fu ON fu.id = SUBSTRING_INDEX(SUBSTRING_INDEX(concat(u.parent_ids,u.id,','), ',', - 3),',',1) ");
        sb.append("left JOIN t_unit su ON su.id = SUBSTRING_INDEX(SUBSTRING_INDEX(concat(u.parent_ids,u.id,','), ',', - 2),',',1) ");
        sb.append("where t.is_del = 0 and u.is_del = 0 ");
        sb.append("and (t.id = ? or t.creator = ? or u.parent_ids like concat(?,'%')) ");
        arrList.add(UserUtils.getUser().getId());
        arrList.add(UserUtils.getUser().getId());
        arrList.add(UserUtils.getUser().getUnit().getParent_ids() + UserUtils.getUser().getUnit().getId() + ",");
        if (!StringUtil.isEmpty(user.getLogin_name())) {
            sb.append("and login_name like concat('%',?,'%')");
            arrList.add(user.getLogin_name());
        }
        if (!StringUtil.isEmpty(user.getName())) {
            sb.append("and name like concat('%',?,'%')");
            arrList.add(user.getName());
        }
        if (!StringUtil.isEmpty(user.getMobile())) {
            sb.append("and mobile like concat('%',?,'%')");
            arrList.add(user.getMobile());
        }
        if (user.getUnit_id() != null) {
            sb.append("and unit_id = ? ");
            arrList.add(user.getUnit_id());
        }
        if (user.getStatus() != null) {
            sb.append("and status = ? ");
            arrList.add(user.getStatus());
        }
        if (user.getLogin_flag() != null) {
            sb.append("and login_flag = ? ");
            arrList.add(user.getLogin_flag());
        }
        if (!StringUtil.isEmpty(user.getStartTime())) {
            sb.append("and create_time >= ? ");
            arrList.add(user.getStartTime());
        }
        if (!StringUtil.isEmpty(user.getEndTime())) {
            sb.append("and create_time <= ? ");
            arrList.add(user.getEndTime());
        }
        //dataScopeFilter(UserUtils.getUser().getId(), sb, arrList, "u", "t");
        int num = getListJdbcSize(sb.toString(), arrList);
        pageBean.setTotalCount(num);
        pageBean.setTotalPage(pageBean.getTotalPage());
        if (num > 0) {
            sb.append(" order by t.id desc limit ?,?");
            arrList.add((pageBean.getCurPage() - 1) * pageBean.getPageSize());
            arrList.add(pageBean.getPageSize());
            pageBean.setMapList(getListJdbc(sb.toString(), arrList));
        }
        return pageBean;
    }

    /**
     * 删除用户
     *
     * @param id
     * @return
     */
    public int deleteUser(Integer id) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("update t_user set is_del = 1 where id = ? ");
        arrList.add(id);
        return updateJdbc(sb.toString(), arrList);
    }

    /**
     * 编辑用户
     *
     * @param user
     * @return
     */
    public int updateUser(User user) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("update t_user set update_time = ?,updater = ? ");
        arrList.add(user.getUpdate_time());
        arrList.add(user.getUpdater());
        if (!StringUtil.isEmpty(user.getLogin_name())) {
            sb.append(",login_name = ? ");
            arrList.add(user.getLogin_name());
        }
        if (user.getLogin_flag() != null) {
            sb.append(",login_flag = ? ");
            arrList.add(user.getLogin_flag());
        }
        if (!StringUtil.isEmpty(user.getName())) {
            sb.append(",name = ? ");
            arrList.add(user.getName());
        }
        if (!StringUtil.isEmpty(user.getMobile())) {
            sb.append(",mobile = ? ");
            arrList.add(user.getMobile());
        }
        if (!StringUtil.isEmpty(user.getEmail())) {
            sb.append(",email = ? ");
            arrList.add(user.getEmail());
        }
        if (!StringUtil.isEmpty(user.getHead_url())) {
            sb.append(",head_url = ? ");
            arrList.add(user.getHead_url());
        }
        if (!StringUtil.isEmpty(user.getPassword())) {
            sb.append(",password = ? ");
            arrList.add(user.getPassword());
        }
        if (user.getUnit_id() != null) {
            sb.append(",unit_id = ? ");
            arrList.add(user.getUnit_id());
        }
        if (user.getLogin_ip() != null) {
            sb.append(",login_ip = ? ");
            arrList.add(user.getLogin_ip());
        }
        if (user.getLogin_time() != null) {
            sb.append(",login_time = ? ");
            arrList.add(user.getLogin_time());
        }
        if (user.getStatus() != null){
            sb.append(",status = ? ");
            arrList.add(user.getStatus());
        }
        if (!StringUtil.isEmpty(user.getOpenid())) {
            sb.append(",openid = ? ");
            arrList.add(user.getOpenid());
        }
        sb.append(" where id = ?");
        arrList.add(user.getId());
        return updateJdbc(sb.toString(), arrList);
    }

    /**
     * 根据角色获取用户
     * @param role_id
     * @return
     */
    public List<Map<String, Object>> getUserByRole(Integer role_id) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("select t.*,IFNULL(fu.unit_name,u.unit_name) first_unit,su.unit_name second_unit from t_user t ");
        sb.append("join t_user_role ur on ur.user_id = t.id ");
        sb.append("join t_role r on r.id = ur.role_id ");
        sb.append("join t_unit u on u.id = t.unit_id ");
        sb.append("left JOIN t_unit fu ON fu.id = SUBSTRING_INDEX(SUBSTRING_INDEX(concat(u.parent_ids,u.id,','), ',', - 3),',',1) ");
        sb.append("left JOIN t_unit su ON su.id = SUBSTRING_INDEX(SUBSTRING_INDEX(concat(u.parent_ids,u.id,','), ',', - 2),',',1) ");
        sb.append("where t.is_del = 0 and r.is_del = 0 and u.is_del = 0 and r.id = ? ");
        arrList.add(role_id);
        dataScopeFilter(UserUtils.getUser().getId(), sb, arrList, "u", "t");
        return getListJdbc(sb.toString(), arrList);
    }

    /**
     * 根据openid获取
     * @param openid
     * @return
     */
    public User getByOpenid(String openid){
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("select t.* from t_user t join t_unit u on u.id = t.unit_id " +
                "where t.is_del = 0 and t.status = 1 and u.is_del = 0 and u.status = 1 and t.openid = ? ");
        arrList.add(openid);
        return findOne(sb.toString(), arrList);
    }

    /**
     * 根据id获取
     * @param id
     * @return
     */
    public User get(Integer id){
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("select t.* from t_user t ");
        sb.append("join t_unit u on u.id = t.unit_id ");
        sb.append("where t.is_del = 0 and u.is_del = 0 and t.id = ?");
        arrList.add(id);
        return findOne(sb.toString(), arrList);
    }

    /**
     * 根据id获取
     * @param id
     * @return
     */
    public Map<String, Object> getUserMap(Integer id){
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("select t.*,r.name role_name,r.id role_id from t_user t ");
        sb.append("join t_unit u on u.id = t.unit_id ");
        sb.append("left join t_user_role ur on ur.user_id = t.id ");
        sb.append("left join t_role r on r.id = ur.role_id ");
        sb.append("where t.is_del = 0 and u.is_del = 0 and t.id = ? limit 1");
        arrList.add(id);
        List<Map<String, Object>> list = getListJdbc(sb.toString(), arrList);
        if(list != null && list.size() > 0){
            return list.get(0);
        }
        return null;
    }

    /**
     * 根据联系方式获取
     * @param mobile
     * @return
     */
    public User getByMobile(String mobile, boolean available){
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("select t.* from t_user t ");
        if(available){
            sb.append("join t_unit u on u.id = t.unit_id ");
        }
        sb.append("where t.mobile = ? ");
        if(available){
            sb.append("and t.is_del = 0 and t.status = 1 and u.is_del = 0 and u.status = 1 ");
        }
        arrList.add(mobile);
        return findOne(sb.toString(), arrList);
    }

}
